package com.devildogs.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collection;
import javax.sql.DataSource;

/**
 *
 * @author Harry Stefanski
 */
public abstract class AbstractDAO {

    public int update(Object airplaneToUpdate, String tableName) {
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;
        int rowsAffected = 0;
        try {
            conn = getDataSource().getConnection();
            st = conn.createStatement();
            rowsAffected = st.executeUpdate(buildUpdateString(airplaneToUpdate, tableName));
        } catch (SQLException sqle) {
            sqle.printStackTrace();
        } finally {
            closeConnection(rs, st, conn);

        }
        return rowsAffected;
    }

    /**
     * Closes the <code>ResultSet</code>, <code>Statement</code>, and <code>Connection</code> object references.
     *
     * @param rs <code>ResultSet</code> object reference
     * @param st <code>Statement</code> object reference
     * @param conn <code>Connection</code> object reference
     */
    public void closeConnection(ResultSet rs, Statement st, Connection conn) {
        try {
            if (rs != null) {
                rs.close();
            }
            if (st != null) {
                st.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException sqle) {
        }
    }

    public Object getSingle(Object obj) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Object getSingle(int primaryKey) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public int remove(Object obj) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public int remove(int primaryKey) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public int removeMany(Collection<Object> objs) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public int insert(Object obj) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public int insertMany(Collection<Object> objs) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Collection<Object> getAll() throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    abstract public DataSource getDataSource();

    abstract String buildUpdateString(Object airplaneToUpdate, String tableName);
}
